<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      show-checkbox
      node-key="id"
      :default-checked-keys="defaultKey"
      :props="defaultProps"
      check-strictly
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="onSubmit">确定</el-button>
      <el-button @click="onSubmit">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import arrayToTree from 'array-to-tree'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  name: '',
  props: ['roleId'],
  data() {
    return {
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      defaultKey: [],
      changeKey: []
    }
  },
  computed: {},
  created() {
    this.getPermission()
  },
  methods: {
    async getPermission() {
      const res = await getPermissionList()
      this.list = arrayToTree(res.data, {
        parentProperty: 'pid',
        customID: 'id'
      })
      const res2 = await getRoleDetail(this.roleId)
      this.defaultKey = res2.data.permIds
    },
    async onSubmit() {
      this.changeKey = this.$refs.tree.getCheckedKeys()
      await assignPerm({
        id: this.roleId,
        permIds: this.changeKey
      })
      this.$emit('close')
    }
  }
}
</script>
<style lang="less" scoped></style>
